<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no, viewport-fit=cover">
    <title>画板</title>
    <link rel="stylesheet" href="style.css">
<body>
<div class="wrapper" >
<canvas id="canvas" width="100"  height="100"</canvas>
</div>
<script>
    let canvas = document.getElementById('canvas');
    canvas.width = document.documentElement.clientWidth//宽度变成文档的宽度
    canvas.height = document.documentElement.clientHeight//高度变成文档的高度
    let ctx = canvas.getContext('2d');
    ctx.fillStyle = 'black';//填充样式
    ctx.strokeStyle = 'none'//描边样式
    ctx.lineWidth = 10;
    ctx.lineCap = "round";
    function drawLine(x1,y1,x2,y2){
        ctx.beginPath();
        ctx.moveTo(x1, y1);
        ctx.lineTo(x2, y2);
        ctx.stroke();//描边
    }


    let painting = false
    let last
    let isTouchDevice = 'ontouchstart' in
        document.documentElement;
    if(isTouchDevice){
        canvas.ontouchstart = (e) => {
            let x = e.touches[0].clientX
            let y = e.touches[0].clientY
            last = [x,y]
        }
        canvas.ontouchmove = (e) => {
            let x = e.touches[0].clientX
            let y = e.touches[0].clientY
            drawLine(last[0],last[1],x,y)
            last = [x,y]
        }



    }else{

        canvas.onmousedown = (e) =>{
            painting = true
            last = [e.clientX,e.clientY]

        }
        canvas.onmousemove = (e) => {
            if(painting === true){
                drawLine(last[0],last[1],e.clientX,e.clientY)
                last = [e.clientX,e.clientY]
            }
        }
        canvas.onmouseup = () => {
            painting = false
        }

    }

</script>
</body>
</html>